const Controller = require('egg').Controller;
let studentList = [
    { id: "1", name: "男刀", age: "102" },
    { id: "2", name: "阿卡丽", age: "45" },
    { id: "3", name: "卢锡安", age: "45" },
    { id: "4", name: "诺手", age: "112" },
]
class StudentsController extends Controller {
    // 查看数据
    async index() {
        this.ctx.body = studentList
    }
    // 添加
    create() {
        let student = this.ctx.request.body.student;
        studentList.push(student)
        this.ctx.body = true
    }
    // 删除
    destroy() {
        let id = this.ctx.params.id;
        // studentList.splice(id, 1)
        // this.ctx.body = true
        studentList.map((item, index) => {
            if (item.id === id) {
                studentList.splice(index, 1);
            }
        })
        this.ctx.body = true
    }
    // 修改
    update() {
        let id = this.ctx.params.id;
        let student = this.ctx.request.body.student;
        // studentList.splice(id, 1, student)
        studentList.map((item, index) => {
            if (item.id === id) {
                studentList.splice(index, 1,student);
            }
        })
        this.ctx.body = true
    }
}

module.exports = StudentsController;
